//
//  SeJueTestResultViewController.m
//  安好
//
//  Created by qinlongqing on 14-9-16.
//  Copyright (c) 2014年 冯凯. All rights reserved.
//

#import "SeJueTestResultViewController.h"
#import "AppConfig.h"

@interface SeJueTestResultViewController ()
{
    UIImage *_imageV;
    
    CGFloat nameAndFenShuLabelHeight;
    CGFloat levelImageViewHeight;
    CGFloat dateAndSealViewHeight;
    CGFloat shareBTNHeight;
    CGFloat tiShiBGHeight;
}
@end

@implementation SeJueTestResultViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"测试报告";
    
    self.view.backgroundColor = UIColorFromRGB(0xe7f1f4);

    [self makeNavBar];
    
    if (kScreenWidth == 414) {
        nameAndFenShuLabelHeight = 36;
        levelImageViewHeight = 204;
        dateAndSealViewHeight = 96;
        shareBTNHeight = 54;
        tiShiBGHeight = 120;
    }
    else{
        nameAndFenShuLabelHeight = 30;
        levelImageViewHeight = 170;
        dateAndSealViewHeight = 80;
        shareBTNHeight = 45;
        tiShiBGHeight = 100;
    }
    
    CGFloat space = 0;
    if ([[self.testResultDict valueForKey:kSeJueTestType] integerValue] == 1) {//正常
        space = (kScreenHeight - 64 - nameAndFenShuLabelHeight - levelImageViewHeight - dateAndSealViewHeight - shareBTNHeight)/5;
    }
    else{
        space = (kScreenHeight - 64 - nameAndFenShuLabelHeight - levelImageViewHeight - dateAndSealViewHeight - tiShiBGHeight)/5;
    }
    
    UILabel *nameAndFenShuLabel = [[UILabel alloc]init];
    nameAndFenShuLabel.frame = CGRectMake(0, space, kScreenWidth, nameAndFenShuLabelHeight);
    if (kScreenWidth == 414) {
        nameAndFenShuLabel.font = [UIFont systemFontOfSize:17];
    }
    else{
        nameAndFenShuLabel.font = [UIFont systemFontOfSize:15];
    }
    nameAndFenShuLabel.textAlignment = NSTextAlignmentCenter;
    nameAndFenShuLabel.textColor = [UIColor blackColor];
    nameAndFenShuLabel.adjustsFontSizeToFitWidth = YES;
    nameAndFenShuLabel.backgroundColor = [UIColor clearColor];
    NSString *seJueTestResult = [self.testResultDict valueForKey:kSeJueTestResult];
    NSString *seJueTypeString = [AppConfig getSeJueTestResultWithType:[seJueTestResult integerValue]];
    NSString *showStr = [NSString stringWithFormat:@"%@的色觉报告:%@",[self.testResultDict valueForKey:@"testUserName"],seJueTypeString];
    NSRange leftRange = [showStr rangeOfString:@"的色觉报告:"];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:showStr];
    [attributedString addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x4fc1e9) range:NSMakeRange(leftRange.location+leftRange.length, showStr.length - leftRange.location - leftRange.length)];
    nameAndFenShuLabel.attributedText = attributedString;
    [self.view addSubview:nameAndFenShuLabel];

    UIImageView *levelImageView = [[UIImageView alloc]init];
    levelImageView.frame = CGRectMake((kScreenWidth - levelImageViewHeight)/2, nameAndFenShuLabel.frame.origin.y + nameAndFenShuLabel.frame.size.height + space, levelImageViewHeight, levelImageViewHeight);
    if ([[self.testResultDict valueForKey:kSeJueTestType] integerValue] == 1) {
        levelImageView.image = [UIImage imageNamed:@"se_jue_zhengChang"];
    }
    else if ([[self.testResultDict valueForKey:kSeJueTestType] integerValue] == 2){
        levelImageView.image = [UIImage imageNamed:@"se_jue_xian_tian_xing_seMang"];
    }
    else if ([[self.testResultDict valueForKey:kSeJueTestType] integerValue] == 3){
        levelImageView.image = [UIImage imageNamed:@"se_jue_hou_tian_xing_seMang"];
    }
    else if ([[self.testResultDict valueForKey:kSeJueTestType] integerValue] == 4){
        levelImageView.image = [UIImage imageNamed:@"se_jue_quanSeMang"];
    }
    [self.view addSubview:levelImageView];

    //---------------------时间和盖章----------------------
    UIView *dateAndSealView = [[UIView alloc]init];
    dateAndSealView.frame = CGRectMake(0, levelImageView.frame.origin.y + levelImageView.frame.size.height + space, kScreenWidth, dateAndSealViewHeight);
    [self.view addSubview:dateAndSealView];
    
    UILabel *dateLabel = [[UILabel alloc]init];
    if (kScreenWidth == 414) {
        dateLabel.frame = CGRectMake(kScreenWidth*(170.0/320.0), 50, 168, 36);
        dateLabel.font = [UIFont systemFontOfSize:15];
    }
    else{
        dateLabel.frame = CGRectMake(kScreenWidth*(170.0/320.0), 45, 140, 30);
        dateLabel.font = [UIFont systemFontOfSize:13];
    }
    dateLabel.backgroundColor = [UIColor clearColor];
    dateLabel.textAlignment = NSTextAlignmentCenter;
    dateLabel.textColor = [UIColor grayColor];
    
    NSDate *dates = [NSDate date];
    NSDateFormatter *formatter =  [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年MM月dd日  HH:mm"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/beijing"];
    [formatter setTimeZone:timeZone];
    NSString *loctime = [formatter stringFromDate:dates];
    
    dateLabel.text = loctime;
    [dateAndSealView addSubview:dateLabel];
    
    UIImageView *sealView = [[UIImageView alloc]init];
    if (kScreenWidth == 414) {
        sealView.frame = CGRectMake(kScreenWidth*(233.0/320.0), 0, 80, 80);
    }
    else{
        sealView.frame = CGRectMake(kScreenWidth*(233.0/320.0), 0, 67, 67);
    }
    sealView.image = [UIImage imageNamed:@"ce_shi_bao_gao_seal"];
    [dateAndSealView addSubview:sealView];
    
    if ([[self.testResultDict valueForKey:kSeJueTestType] integerValue] == 1) {
        UIButton *shareBTN = [[UIButton alloc]init];
        shareBTN.layer.cornerRadius = 3;
        if (kScreenWidth == 414) {
            shareBTN.frame = CGRectMake(20, dateAndSealView.frame.origin.y + dateAndSealView.frame.size.height + space, kScreenWidth - 40, shareBTNHeight);
        }
        else{
            shareBTN.frame = CGRectMake(15, dateAndSealView.frame.origin.y + dateAndSealView.frame.size.height + space, kScreenWidth - 30, shareBTNHeight);
        }
        [shareBTN setBackgroundImage:[UIImage imageNamed:@"tiJianFenXiangBG"] forState:UIControlStateNormal];
        [shareBTN setBackgroundImage:[UIImage imageNamed:@"tiJianFenXiangBG"] forState:UIControlStateHighlighted];
        [shareBTN setTitle:@"分享" forState:UIControlStateNormal];
        [shareBTN setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [shareBTN addTarget:self action:@selector(doToShare:) forControlEvents:UIControlEventTouchUpInside];
        if ([[UserManager shareInstance] getUserIsLogin].length > 0) {
            shareBTN.hidden = NO;
        }
        else{
            shareBTN.hidden = YES;
        }
        [self.view addSubview:shareBTN];
    }
    else{
        UIImageView *tiShiBG = [[UIImageView alloc]init];
        tiShiBG.frame = CGRectMake(0, kScreenHeight - 64 - tiShiBGHeight, kScreenWidth, tiShiBGHeight);
        tiShiBG.image = [UIImage imageNamed:@"test_result_fen_xiang_bg"];
        [self.view addSubview:tiShiBG];
        
        UILabel *wenXinTiShiLabel = [[UILabel alloc]init];
        if (kScreenWidth == 414) {
            wenXinTiShiLabel.frame = CGRectMake(32, 27, 100, 18);
            wenXinTiShiLabel.font = [UIFont systemFontOfSize:14];
        }
        else{
            wenXinTiShiLabel.frame = CGRectMake(27, 23, 100, 15);
            wenXinTiShiLabel.font = [UIFont systemFontOfSize:12];
        }
        wenXinTiShiLabel.textAlignment = NSTextAlignmentLeft;
        wenXinTiShiLabel.textColor = UIColorFromRGB(0x4fc1e9);
        wenXinTiShiLabel.text = @"温馨小提示";
        wenXinTiShiLabel.backgroundColor = [UIColor clearColor];
        [tiShiBG addSubview:wenXinTiShiLabel];
        
        UILabel *tiShiLabel = [[UILabel alloc]init];
        if (kScreenWidth == 414) {
            tiShiLabel.frame = CGRectMake(45, 51, kScreenWidth*(255.0/320.0), 48);
            tiShiLabel.font = [UIFont systemFontOfSize:14];
        }
        else{
            tiShiLabel.frame = CGRectMake(38, 43, kScreenWidth*(255.0/320.0), 40);
            tiShiLabel.font = [UIFont systemFontOfSize:12];
        }
        tiShiLabel.textAlignment = NSTextAlignmentLeft;
        tiShiLabel.textColor = UIColorFromRGB(0x4fc1e9);
        tiShiLabel.lineBreakMode = NSLineBreakByWordWrapping;
        tiShiLabel.numberOfLines = 2;
        tiShiLabel.text = @"    如果您对测试结果有所顾虑，建议您去附近的公立医院做相关的检查和诊疗。";
        tiShiLabel.backgroundColor = [UIColor clearColor];
        [tiShiBG addSubview:tiShiLabel];
    }
    
}

- (void)makeNavBar{
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(doBack)];
    [backBtnImg setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}

-(void)doBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)doToShare:(UIButton *)sender{
    CGRect rect = CGRectMake(0, 0, kScreenWidth, sender.frame.origin.y);
//    if (IPHONE5_OR_LATER) {
//        rect = CGRectMake(0, 0, 320, 375);
//    }
//    else{
//        rect = CGRectMake(0, 0, 320, 325);
//    }
    
    [self imageFromView:self.view atFrame:rect];
    
//分享
    [self showShareView];
}

- (void)imageFromView:(UIView *)theView atFrame:(CGRect)r
{
    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [theView.layer renderInContext:context];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGImageRef imageRef = theImage.CGImage;
    CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, r);//原图中抠出来部分图
    UIImage *subImage = [UIImage imageWithCGImage:subImageRef];//新图
    _imageV = subImage;
//    UIGraphicsBeginImageContext(theView.frame.size);
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSaveGState(context);
//    UIRectClip(r);
//    [theView.layer renderInContext:context];
//    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    
//    _imageV = theImage;
//    //已传过来值，从这里开始写测试报告
}
#pragma 实现share方法，通过点击的视图的tag值判断是什么类型的分享
- (void)share:(UITapGestureRecognizer *)shareTap{
    switch (shareTap.view.tag) {
        case 11:{//微信
            [self weixinShare:kWeiXinShareUrl :kWeiXinShareTitle :kWeiXinShareText :_imageV];
        }
            break;
        case 12:{//朋友圈
            [self weixinFriendShare:kWeiXinFriendShareUrl :kWeiXinFriendShareTitle :kWeiXinFriendShareText :_imageV];
        }
            break;
        case 13:{//短信
            [self showSMSPicker:kWeiXinFriendShareUrl :kWeiXinFriendShareTitle :kWeiXinFriendShareText :nil];
        }
            break;
        case 14:{//QQ
            [self qqShare:kQQshareUrl :kQQshareTitle :kQQshreDescription :_imageV];
        }
            break;
        case 15:{//QQ空间
            [self qqZoneShare:kQQZoneShareUrl :kQQZoneShareTitle :kQQZoneShareDescription :_imageV];
        }
            break;
        case 16:{//新浪微博
            [self sinaWeiboShare:kSinaWeiboShareUrl :kSinaWeiboShareTitle :kSinaWeiboShareText :_imageV];
        }
            break;
            
        default:
            break;
    }
}


- (CGRect)getSeJuePositionWithTestResult:(NSInteger)seJueTestResultType{
    CGRect rect;
    if (seJueTestResultType == 1) {//正常
        if (IPHONE5_OR_LATER) {
            rect = CGRectMake(90, 86, 50, 31);
        }
        else{
            rect = CGRectMake(90, 56, 50, 31);
        }
    }
    else if (seJueTestResultType == 2){
        if (IPHONE5_OR_LATER) {
            rect = CGRectMake(90, 86 + 155, 50, 31);
        }
        else{
            rect = CGRectMake(90, 56 + 155, 50, 31);
        }
    }
    else if (seJueTestResultType == 3){//后天性
        if (IPHONE5_OR_LATER) {
            rect = CGRectMake(185, 86 + 155, 50, 31);
        }
        else{
            rect = CGRectMake(185, 56 + 155, 50, 31);
        }
    }
    else if (seJueTestResultType == 4){//全色盲
        if (IPHONE5_OR_LATER) {
            rect = CGRectMake(185, 86, 50, 31);
        }
        else{
            rect = CGRectMake(185, 56, 50, 31);
        }
    }
    return rect;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
